#!/usr/bin/env coffee

import Searcher from 'node-ip2region'
import thisdir from '@rmw/thisfile'
import path from 'path'
import lines from './lines'
import Csv from './csv'

DIR = path.dirname(path.dirname(
  thisdir(`import.meta`)
))

do =>
  searcher = Searcher.create()
  csv = Csv(path.join(DIR,"out"))
  csv.write [
    'ip'
    'city_id'
  ]
  for await line from lines(path.join(DIR, 'ip.csv'))
    try
      r = searcher.btreeSearchSync(line)
    catch err
      continue
    csv.write [
      line
      r.city
    ].concat r.region.split('|')

  csv.end()

